﻿(function($) {
    function bw(_config) {
        this.config = _config || {
            api: "http://192.168.0.212:8050/api/",
            sorket: "", //websorket 地址  后台推送消息给微信页面
            wxAppId: "wxe69910f2a2cfd1a1",
            secret: "f1b5e5b308b515525ffcdd31db07845a",
            wxCallBackUrl: "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxe69910f2a2cfd1a1&redirect_uri=index.html" + "&response_type=code&scope=snsapi_base&state=11#wechat_redirect",
            baiduMap: {
                ak: "BORW59HeNs0amhebvmy1qvrwySYgcWDI", //百度地图开发者 web AK
                service_id: "132506", //服务ID
                suggestionUrl: "http://api.map.baidu.com/place/v2/suggestion/", //百度地图建议地址 http://lbsyun.baidu.com/index.php?title=webapi/place-suggestion-api
                trackHistoryUrl: "http://api.map.baidu.com/trace/v2/track/gethistory", //查询某一个监控对象一天之内轨迹接口
                entityListUrl: "http://api.map.baidu.com/trace/v2/entity/list"
            }
        };
    }
    /**
     * [setItem description]
     * 向localStorage存数据
     * 
     * @author  fengzhaoquan@bestwise.com
     * @DateTime 2017-01-12T16:53:27+0800
     * @param    {[type]}                 key  [description]
     * @param    {[type]}                 data [description]
     */
    bw.prototype.setItem = function(key, data) {
        localStorage.setItem(key, JSON.stringify(data));
    }

    /**
     * [getItem description]
     * 从localStorage取数据
     * 
     * @author  fengzhaoquan@bestwise.com
     * @DateTime 2017-01-12T16:55:03+0800
     * @param    {[type]}                 key [description]
     * @return   {[type]}                     [description]
     */
    bw.prototype.getItem = function(key) {
        return localStorage.getItem(key) ? JSON.parse(localStorage.getItem(key)) : null;
    }

    /**
     * [setRootElementFontSize description]
     * 设置根节点字体大小
     * 
     * @author  fengzhaoquan@bestwise.com
     * @DateTime 2017-01-12T16:56:15+0800
     * @param    {[type]}                 sejiPx  [设计稿尺寸 默认750]
     * @param    {[type]}                 jibenPx [一个rem换算尺寸 默认16]
     */
    bw.prototype.setRootElementFontSize = function(sejiPx, jibenPx) {
        sejiPx = sejiPx || 750;
        jibenPx = jibenPx || 16;
        var winH = document.documentElement.clientHeight;
        var winW = document.documentElement.clientWidth;
        document.documentElement.style.fontSize = winW / sejiPx * jibenPx + 'px';
    }

    /**
     * [getUrlParams description]
     * 获取传入url地址中的参数对象，否则计算当前页面的url
     * 
     * @author  fengzhaoquan@bestwise.com
     * @DateTime 2017-01-12T16:59:39+0800
     * @param    {[type]}                 url [url地址]
     * @return   {[type]}                     [map]
     */
    bw.prototype.getUrlParams = function(url) {
        var args = {};
        url = url || location.href;
        var pairs = url.split("?")[1].split('&');
        for (var i = 0; i < pairs.length; i++) {
            var pos = pairs[i].split('=');
            if (pos.length == 1) {
                continue;
            }
            args[pos[0]] = decodeURIComponent(pos[1]);
        }
        return args;
    }

    /**
     * [isWxBower description]
     * 判断当前浏览器是否微信的内核
     * 
     * @author  fengzhaoquan@bestwise.com
     * @DateTime 2017-01-12T17:04:18+0800
     * @return   {Boolean}                [description]
     */
    bw.prototype.isWxBower = function() {
        var ua = navigator.userAgent.toLowerCase();
        return ua.match(/MicroMessenger/i) == "micromessenger";
    }

    /**
     * [getUserAgent description]
     * 判断用户的设备类型
     * 
     * @author  fengzhaoquan@bestwise.com
     * @DateTime 2017-01-13T03:07:19+0800
     * @return   {[type]}                 [description]
     */
    bw.prototype.getUserAgent = function() {
        var ua = navigator.userAgent.toLowerCase();
        if (ua.match(/iPhone\sOS/i) == "iphone os") {
            return "ios";
        } else if (ua.match(/Android/i) == "android") {
            return "android";
        } else {
            return "other";
        }
    }
    bw.prototype.formateToDate = function(source, char) {
        var result = "";
        try {
            var _t = new Date(source);
            result = _t.getFullYear() + char + buling(_t.getMonth() + 1) + char + buling(_t.getDate());
        } catch (ex) {
            result = "";
        }
        return result;
    }
    bw.prototype.formateToTime = function(source, char) {
        var result = "";
        try {
            var _t = new Date(source);
            result = buling(_t.getHours()) + char + buling(_t.getMinutes());
        } catch (ex) {
            result = "";
        }
        return result;
    }

    function buling(no) {
        return no < 10 ? "0" + no : no;
    }

    window.$bw = new bw();
})($);
